﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using EPE.Common;
using EPE.DBUtility;
using EPE.IDAL;
namespace EPE.MySqlDAL
{
    public class AuthenticationImpl : IAuthentication
    {
        /// <summary>
        /// 检查用户密码是否正确
        /// </summary>
        /// <param name="argLoginName"></param>
        /// <param name="argPassword"></param>
        /// <returns></returns>
        public DataSet LoginCheckPassWord(string argLoginName, string argPassword)
        {
            string strSQL;

            DataSet retDS = new DataSet();

            DataBaseTool clsDbTool = new DataBaseTool();

            try
            {
                strSQL = " SELECT count(UserId) AS c ";
                strSQL = strSQL + " FROM tbl_User ";
                strSQL = strSQL + " WHERE 1=1 ";
                strSQL = strSQL + " AND account=@LoginName ";
                strSQL = strSQL + " AND Password=@Password";

                clsDbTool.ParameterClear();
                clsDbTool.ParameterAdd("LoginName", argLoginName);
                clsDbTool.ParameterAdd("Password", ApCommonModel.md5(argPassword, 32));
                clsDbTool.CommandText = strSQL;
                clsDbTool.Open();

                retDS = clsDbTool.GetDataSet();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                clsDbTool.Close();
            }
            return retDS;


        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="argLoginName"></param>
        /// <returns></returns>
        public DataSet GetUserInfo(string argLoginName) {

            string strSQL;

            DataSet retDS = new DataSet();

            DataBaseTool clsDbTool = new DataBaseTool();

            try
            {
                strSQL = " SELECT u.userid,u.account,u.username,u.departmentid,u.postid,p.postname,d.departmentname,c.companyid,c.companyname,c.areaid ";
                strSQL = strSQL + " FROM tbl_user u,tbl_department d,tbl_company c,tbl_post p ";
                strSQL = strSQL + " WHERE 1=1 ";
                strSQL = strSQL + " AND u.departmentid=d.departmentid";
                strSQL = strSQL + " AND d.companyid=c.companyid";
                strSQL = strSQL + " AND p.companyid=c.companyid";
                strSQL = strSQL + " AND u.postid=p.postid";
                strSQL = strSQL + " AND account=@LoginName ";

                clsDbTool.ParameterClear();
                clsDbTool.ParameterAdd("LoginName", argLoginName);
                clsDbTool.CommandText = strSQL;
                clsDbTool.Open();

                retDS = clsDbTool.GetDataSet();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                clsDbTool.Close();
            }
            return retDS;
        }


    }
}
